package com.weimob.mcloud.wechatwork.customer.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.weimob.elasticjob.annotation.ElasticJob;
import com.weimob.elasticjob.executor.context.ExecutorContext;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.service.privatesphere.PrivateSphereService;
import com.weimob.mcloud.wechatwork.customer.service.TagManageService;
import com.weimob.soa.common.response.SoaResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2022/8/10 1:56 下午
 */
@Slf4j
@Component
public class TagJob {

    @Autowired
    private TagManageService tagManageService;

    @Reference
    private PrivateSphereService privateSphereService;

    @ElasticJob(name = "syncTagFromQW", cron = "0 */1 * * * ?")
    public void syncTag(ExecutorContext context) {
        Map<String, String> customParameter = context.getTriggerCustomParameter();
        if (MapUtils.isEmpty(customParameter)) {
            return;
        }

        log.info("触发标签信息同步, request:{}", JSON.toJSONString(customParameter));

        String corpId = customParameter.get("corpId");
        String bosId = customParameter.get("bosId");
        if (StringUtils.isBlank(corpId) || StringUtils.isBlank(bosId)) {
            return;
        }

        PrivateSphere privateSphereReq = new PrivateSphere();
        privateSphereReq.setCorpId(corpId);
        privateSphereReq.setBosId(Long.parseLong(bosId));
        privateSphereReq.setProductId(203L);

        SoaResponse<PrivateSphere, Void> sphereVoidSoaResponse = privateSphereService.fetch(privateSphereReq);
        if (0L != Long.parseLong(sphereVoidSoaResponse.getReturnCode())) {
            log.error("查询私域失败, request:{}", JSON.toJSONString(privateSphereReq));
            return;
        }

        PrivateSphere responseVo = sphereVoidSoaResponse.getResponseVo();
        if (Objects.isNull(responseVo)) {
            return;
        }

        tagManageService.initTag(responseVo, true);

    }

    @ElasticJob(name = "syncTagByCorpIdsFromQW", cron = "0 */1 * * * ?")
    public void syncTagByCorpIdsFromQW(ExecutorContext context) {

        Map<String, String> customParameter = context.getTriggerCustomParameter();
        if (MapUtils.isEmpty(customParameter)) {
            return;
        }

        log.info("批量触发客户标签信息同步, request:{}", JSON.toJSONString(customParameter));

        String corpId = customParameter.get("corpId");
        if (StringUtils.isBlank(corpId)) {
            return;
        }

        String bosId = customParameter.get("bosId");
        if (StringUtils.isBlank(bosId)) {
            return;
        }

        PrivateSphere privateSphereReq = new PrivateSphere();
        privateSphereReq.setCorpId(corpId);
        privateSphereReq.setBosId(Long.parseLong(bosId));
        privateSphereReq.setProductId(203L);

        SoaResponse<PrivateSphere, Void> sphereVoidSoaResponse = privateSphereService.fetch(privateSphereReq);
        if (0L != Long.parseLong(sphereVoidSoaResponse.getReturnCode())) {
            log.error("查询私域失败, request:{}", JSON.toJSONString(privateSphereReq));
            return;
        }
        PrivateSphere responseVo = sphereVoidSoaResponse.getResponseVo();
        if (Objects.isNull(responseVo)) {
            return;
        }


        try {
            log.info("批量触发客户标签信息同步start, corpId:{}", corpId);

            tagManageService.initTag(responseVo, true);

            log.info("批量触发客户标签信息同步end, corpId:{}", corpId);
        } catch (Exception e) {

            log.error("批量触发客户标签信息同步失败, corpId:{}", corpId);
        }
    }

    @ElasticJob(name = "syncCustomerTagFromQW", cron = "0 */1 * * * ?")
    public void syncCustomerTag(ExecutorContext context) {
        Map<String, String> customParameter = context.getTriggerCustomParameter();
        if (MapUtils.isEmpty(customParameter)) {
            return;
        }

        log.info("触发客户标签信息同步, request:{}", JSON.toJSONString(customParameter));
        String externalUserId = customParameter.get("externalUserId");

        String bosId = customParameter.get("bosId");
        if (StringUtils.isBlank(bosId)) {
            return;
        }

        PrivateSphere request = new PrivateSphere();
        request.setBosId(Long.parseLong(bosId));
        request.setProductId(203L);
        SoaResponse<PrivateSphere, Void> sphereVoidSoaResponse = privateSphereService.fetch(request);
        if (0L != Long.parseLong(sphereVoidSoaResponse.getReturnCode())) {
            return;
        }
        PrivateSphere privateSphere = sphereVoidSoaResponse.getResponseVo();

        if (Objects.isNull(privateSphere) || StringUtils.isBlank(externalUserId)) {
            return;
        }

        tagManageService.initCustomerTag(privateSphere, externalUserId, null);
    }
}
